// pages/promotion/index/index.js
const app = getApp();
const globalData = getApp().globalData;
const regeneratorRuntime = app.globalData.regeneratorRuntime;
var util = require('../../../utils/util.js');
var doctorservice = require("../../../apiservice/doctor/doctorservice");
Page({

	/**
	 * 页面的初始数据
	 */
	data: {
		statistics: {},
		startDate: util.formatTime(new Date()),
		endDate: util.formatTime(new Date()),
	},
	doctorSuccess: function(res) {

		// res.eventChannel.emit('openedDoctorPage', {
		// 	data: 'detail'
		// })
	},
	/**
	 * 生命周期函数--监听页面加载
	 */
	onLoad: function(options) {
		this.getStatistics();
	},

	async getStatistics() {
		let that = this;
		var currentDate = util.getMonthDate();
		that.setData({
			startDate: currentDate.starttime,
			endDate: currentDate.endtime
		})
		var currStatistics = await doctorservice.QryNetDoctorsStatistic(currentDate.starttime, currentDate
			.endtime);
		that.setData({
			statistics: currStatistics.data,
		});
	},

	/**
	 * 生命周期函数--监听页面初次渲染完成
	 */
	onReady: function() {

	},

	/**
	 * 生命周期函数--监听页面显示
	 */
	onShow: function() {
		if (typeof this.getTabBar === 'function' &&
			this.getTabBar()) {
			this.getTabBar().setData({
				selected: "promotion"
			})
		}

		let userInfo = globalData.userInfo;
		let mobilerole = userInfo.mobilerole_systemuser;
		// if (userInfo.site == "爱尔眼科医院集团") { //集团
		//   let roleIds = [1, 2, 3];
		//   this.showTabBarItem(mobilerole, roleIds, "showScreens");

		//   roleIds = [5, 4, 7];
		//   this.showTabBarItem(mobilerole, roleIds, "showPromotion");
		// }
		// else {  //医院  医院配了筛查，则学术推广也显示
		//   let roleIds = [1, 2, 3];  //筛查
		//   if (util.haveRole(mobilerole, roleIds)) {
		//     if (typeof this.getTabBar === 'function' &&
		//       this.getTabBar()) {
		//       this.getTabBar().setData({
		//         ["showScreens"]: true,
		//         ["showPromotion"]: true
		//       })
		//     }
		//   }
		//   else { //医院没有配置筛查
		//     this.getTabBar().setData({
		//       ["showScreens"]: false
		//     })
		//     roleIds = [5, 4, 7];
		//     this.showTabBarItem(mobilerole, roleIds, "showPromotion");
		//   }
		// }

		// let roleIds = [12, 13];
		// this.showTabBarItem(mobilerole, roleIds, "showFollow");
		let roleIds = [1, 2, 3];
		this.showTabBarItem(mobilerole, roleIds, "showScreens");
		roleIds = [5, 4, 7, 13]; //13: 村医
		this.showTabBarItem(mobilerole, roleIds, "showPromotion");
		roleIds = [12];
		this.showTabBarItem(mobilerole, roleIds, "showFollow");

		//this.onTabItemTap();
	},

	showTabBarItem: function(mobilerole, roleIds, attrName) {
		let that = this;
		if (util.haveRole(mobilerole, roleIds)) {
			if (typeof that.getTabBar === 'function' &&
				that.getTabBar()) {
				that.getTabBar().setData({
					[attrName]: true
				})
			}
		} else {
			if (typeof that.getTabBar === 'function' &&
				that.getTabBar()) {
				that.getTabBar().setData({
					[attrName]: false
				})
			}
		}
	},

	/**
	 * 生命周期函数--监听页面隐藏
	 */
	onHide: function() {

	},

	/**
	 * 生命周期函数--监听页面卸载
	 */
	onUnload: function() {

	},

	/**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
	onPullDownRefresh: function() {
		this.getStatistics().then(() => {
			wx.stopPullDownRefresh()
		});

	},

	/**
	 * 页面上拉触底事件的处理函数
	 */
	onReachBottom: function() {

	},

	/**
	 * 用户点击右上角分享
	 */
	onShareAppMessage: function() {

	},
	//权限控制
	// onTabItemTap: function(item) {
	// 	let userInfo = globalData.userInfo;
	// 	let mobilerole = userInfo.mobilerole_systemuser;
	// 	var roleIds = [5, 4, 7];
	// 	return util.checkRole(mobilerole, roleIds, function() {
	// 		wx.switchTab({
	// 			url: '../../tasks/list/list',
	// 			success: function() {
	// 				wx.showToast({
	// 					title: '权限不足',
	// 					duration: 2500
	// 				})
	// 			}
	// 		});
	// 	});
	// },


})
